package com.kingmed.kmss.admin.modules.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingmed.kmss.admin.modules.sys.model.SysDataDict;
import com.kingmed.kmss.admin.modules.sys.service.SysDataDictService;
import com.kingmed.kmss.common.vo.CommonPage;
import com.kingmed.kmss.common.vo.CommonResponse;
import com.kingmed.kmss.common.vo.CommonResponseHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统数据字典表 前端控制器
 * </p>
 *
 * @author WYL
 * @since 2022-12-14
 */
@Slf4j
@RestController
@RequestMapping("/sys/dict")
@Api(tags = "sys数据字典管理")
public class SysDataDictController {

    @Autowired
    private SysDataDictService sysDataDictService;

    @ApiOperation("创建数据字典")
    @PostMapping(value = "/create")
    public CommonResponse<Integer> create(@Validated @RequestBody List<SysDataDict> dataDictList) {
        return CommonResponseHelper.success(sysDataDictService.create(dataDictList));
    }

    @ApiOperation("修改数据字典")
    @PostMapping(value = "/update")
    public CommonResponse<Integer> update(@Validated @RequestBody List<SysDataDict> dataDictList) {
        return CommonResponseHelper.success(sysDataDictService.update(dataDictList));
    }

    @ApiOperation("删除数据字典(ID)")
    @PostMapping(value = "/deleteById")
    public CommonResponse<Integer> deleteById(@RequestBody List<Long> dataDictIdList) {
        return CommonResponseHelper.success(sysDataDictService.delete(dataDictIdList));
    }

    @ApiOperation("删除数据字典(code)")
    @GetMapping(value = "/deleteByCode")
    public CommonResponse<?> deleteByCode(@RequestParam String dictCode) {
        sysDataDictService.deleteByCode(dictCode);
        return CommonResponseHelper.success();
    }

    @ApiOperation("分页模糊查询(查库)")
    @GetMapping(value = "/list")
    public CommonResponse<CommonPage<SysDataDict>> list(@RequestParam(required = false) String dictNameKey,
                                                        @RequestParam(required = false) String dictValueKey,
                                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<SysDataDict> dictList = sysDataDictService.list(dictNameKey, dictValueKey, pageSize, pageNum);
        return CommonResponseHelper.success(CommonPage.restPage(dictList));
    }

    @ApiOperation("分页查询字典编码")
    @GetMapping(value = "/listCodes")
    public CommonResponse<CommonPage<SysDataDict>> listCodes(@RequestParam(required = false) String dictNameKey,
                                                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<SysDataDict> dictList = sysDataDictService.listCodes(dictNameKey, pageSize, pageNum);
        return CommonResponseHelper.success(CommonPage.restPage(dictList));
    }

    @ApiOperation("精准查询(查库)")
    @PostMapping(value = "/query")
    public CommonResponse<List<SysDataDict>> query(@RequestBody SysDataDict params) {
        return CommonResponseHelper.success(sysDataDictService.query(params));
    }

    @ApiOperation("根据数据字典编码查询")
    @GetMapping(value = "/queryByCode")
    public CommonResponse<List<SysDataDict>> queryByCode(@RequestParam String dictCode) {
        return CommonResponseHelper.success(sysDataDictService.queryByCode(dictCode));
    }

    @ApiOperation("根据数据字典编码批量查询")
    @PostMapping(value = "/queryByCodeBatch")
    public CommonResponse<Map<String, List<SysDataDict>>> queryByCodeBatch(@RequestBody List<String> dictCodes) {
        return CommonResponseHelper.success(sysDataDictService.queryByCodeBatch(dictCodes));
    }

    @ApiOperation("根据数据字典编码+Key查询")
    @GetMapping(value = "/queryByCodeKey")
    public CommonResponse<List<SysDataDict>> queryByCodeKey(@RequestParam String dictCode, @RequestParam String dictKey) {
        return CommonResponseHelper.success(sysDataDictService.queryByCodeKey(dictCode, dictKey));
    }
}
